package com.mdb.open.trade.config;


import com.mdb.bc.autoconfigure.date.AccountingDateProperties;
import com.mdb.bc.utils.DateTimeUtil;
import com.mdb.bc.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @description:
 * @author: you.yichao
 * @create: 2019-08-23 17:28
 **/
@Component
@Primary
public class AccountingDateUtil {

    @Autowired
    AccountingDateProperties accountingDateProperties;

    /**
     * 获取会计日yyyyMMdd
     * @return
     */
    public LocalDate getAccountingDate() {

        String accountingDay = this.accountingDateProperties.getAccountingDay();
        if (ObjectUtil.isEmpty(accountingDay)) {
            return LocalDate.now();
        }

        return LocalDate.parse(accountingDay, DateTimeFormatter.BASIC_ISO_DATE);
    }

    /**
     * 获取会计日yyyyMMdd HH:mm:ss
     * @return
     */
    public LocalDateTime getAccountingDateTime() {

        String accountingDay = this.accountingDateProperties.getAccountingDay();
        if (ObjectUtil.isEmpty(accountingDay)) {
            return LocalDateTime.now();
        }
        LocalTime localTime = LocalTime.now();
        LocalDate localDate = LocalDate.parse(accountingDay, DateTimeFormatter.BASIC_ISO_DATE);
        return LocalDateTime.of(localDate, localTime);
    }

    /**
     * 获取会计日字符串 yyyyMMdd
     * @return
     */
    public String getAccountingDateStr(String formatter) {
        return DateTimeUtil.convert2Str(getAccountingDate(), formatter);
    }

    /**
     * 获取会计日字符串 yyyyMMdd HH:mm:ss
     * @return
     */
    public String getAccountingDateTimeStr(String formatter) {
        return DateTimeUtil.convert2Str(getAccountingDateTime(), formatter);
    }

}
